﻿using Common.Communication;
using Common.Device.DeviceDb;
using Common.Device.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace Scanner
{
    /// <summary>
    /// 民德扫码枪 使用 透传工具232-网口 后通过网口发送指令 
    /// 恢复出厂设置 16 4D 0D 25 25 25 44 45 46 2E
    /// 设置为主机模式 16 4D 0D 30 34 30 31 44 30 35 2E
    /// </summary>
    public class MindeoSocket : SocketClientBase, IScanner
    {
        public string Name { get; set; }
        public MindeoSocket(UseDevice device) : base(device.port, Convert.ToInt32(device.parameter), timeout: 1500)
        {
            Name = device.name;
        }
        /// <summary>
        /// 在主机模式下执行一次
        /// </summary>
        /// <param name="timeout">保持时长（毫秒），填写解码设置：触发超时时间设置的值</param>
        /// <returns></returns>
        public Result<string> ReadOne(int timeout = 1200)
        {
            lock (this)
            {
                socket.ReceiveTimeout = timeout;
                socket.SendTimeout = timeout;
                //相当与发送164D0D16540D2E指令一次
                AppendInfo(Name, "扫码");
                var result = WriteThenRead(Encoding.UTF8.GetString(CmdStart));
                AppendResult(Name, result.Value ?? "空");
                return result;
            }
        }
        byte[] CmdStart => new byte[] { 0x16, 0x4D, 0x0D, 0x16, 0x54, 0x0D, 0x2E };
    }
}
